package hello;

import java.util.*;
import java.math.*;

import org.dom4j.*;
import org.dom4j.io.*;

import org.hibernate.*;
import persistence.*;
import auction.model.*;

public class HelloWorld2OneToMany
{
	
	
	public static void main(String[] args)throws Exception
	{
		
		Session s=HibernateUtil.getSessionFactory().openSession();
		Transaction tx=s.beginTransaction();

		
		Category2OneToMany catA =new Category2OneToMany("cat a "+new Date());
		Item2OneToMany iA=new Item2OneToMany("item a "+new Date());
		
		Category2OneToMany catB =new Category2OneToMany("cat b "+new Date());
		Item2OneToMany iB=new Item2OneToMany("item b "+new Date());
		
		//catA.getItems().add(iA);
		//catA.getItems().add(iB);
		
		iA.getCats().add(catA);
		//iA.getCats().add(catB);
		
		iB.getCats().add(catA);
		
		//s.save(catA);
		//s.save(catB);
		//s.save(iA);
		//s.save(iB);
		
		tx.commit();
		s.close();
		
		
		
		Session s1=HibernateUtil.getSessionFactory().openSession();
		Transaction tx1=s1.beginTransaction();
		
		
		Category2OneToMany catout=(Category2OneToMany)s1.load(Category2OneToMany.class,3L);
		out(catout.getItems().size());
		
		tx1.commit();
		s1.close();
		
		
		
		//HibernateUtil.stat();
		HibernateUtil.shutdown();
	}
	
	public static void getParent(Class clazz)
	{
		Class parent=clazz.getSuperclass();
	  if(parent !=null)
	  {
			System.out.println("###"+parent.getName());
			getParent(parent);
		}
	}
	
	static void out(Object obj)
	{
		System.out.println(obj);
	}
}
